package com.cennavi.bigdata.baotou.util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.Properties;

/**
 * Created by zuoweiguang on 2016/9/7.
 */
public class PropertiesUtil {

    //获取配置文件数据库信息
    public static JSONObject getProperties() {
        String rootPath = PropertiesUtil.class.getClassLoader().getResource("").getPath();
//        String rootPath = System.getProperty("springmvc.root");
//        File directory = new File(rootPath + "/WEB-INF/db.properties");
        System.out.println("root path:" + rootPath);
        File directory = new File(rootPath + "/conf.properties");
        System.out.println("file path:" + directory.toString());
        String filePath = directory.getAbsolutePath();
        Properties props = new Properties();
        InputStream in = null;
        JSONObject propObj = null;

        try {
            in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);

            String RticTraffic = props.getProperty("RticTrafficUrl");
            String CongestionHotspotsUrl = props.getProperty("CongestionHotspotsUrl");
            String RoadIndexRT = props.getProperty("RoadIndexRTUrl");
            String DCIndexRT = props.getProperty("DCIndexRTUrl");
            String HotIndexRT = props.getProperty("HotIndexRTUrl");
            String Pattern = props.getProperty("PatternUrl");
            String TeamIndexRT = props.getProperty("TeamIndexRTUrl");
            String DCIndexByYear = props.getProperty("DCIndexByYearUrl");
            String DCIndexByMonth = props.getProperty("DCIndexByMonthUrl");
            String DCIndexByHoli = props.getProperty("DCIndexByHoliUrl");
            String CongestionRoadRank = props.getProperty("CongestionRoadRank");
            String CongestionRoadStatis = props.getProperty("CongestionRoadStatis");
            String CongestionRoadTime = props.getProperty("CongestionRoadTime");
            String TeamRoadIndexRT = props.getProperty("TeamRoadIndexRTUrl");
            String patternStatus = props.getProperty("patternStatus");
            String diagram = props.getProperty("diagramUrl");
            String weather = props.getProperty("weatherUrl");

            String pgHost = props.getProperty("pgHost");
            int pgPort = Integer.valueOf(props.getProperty("pgPort"));
            String pgDb = props.getProperty("pgDb");
            String pgUser = props.getProperty("pgUser");
            String pgPwd = props.getProperty("pgPwd");

            String mongoHost = props.getProperty("mongoHost");
            int mongoPort = Integer.valueOf(props.getProperty("mongoPort"));
            String mongoDb = props.getProperty("mongoDb");
            String trafficCol = props.getProperty("trafficStatus");
            String correspondingCol = props.getProperty("corresponding");

            propObj = new JSONObject();

            propObj.put("RticTraffic", RticTraffic);
            propObj.put("CongestionHotspotsUrl", CongestionHotspotsUrl);
            propObj.put("RoadIndexRT", RoadIndexRT);
            propObj.put("DCIndexRT", DCIndexRT);
            propObj.put("HotIndexRT", HotIndexRT);
            propObj.put("Pattern", Pattern);
            propObj.put("TeamIndexRT", TeamIndexRT);
            propObj.put("DCIndexByYear", DCIndexByYear);
            propObj.put("DCIndexByMonth", DCIndexByMonth);
            propObj.put("DCIndexByHoli", DCIndexByHoli);
            propObj.put("CongestionRoadRank", CongestionRoadRank);
            propObj.put("CongestionRoadStatis", CongestionRoadStatis);
            propObj.put("CongestionRoadTime", CongestionRoadTime);
            propObj.put("TeamRoadIndexRT", TeamRoadIndexRT);
            propObj.put("patternStatus", patternStatus);
            propObj.put("diagram", diagram);
            propObj.put("weather", weather);

            propObj.put("pgHost", pgHost);
            propObj.put("pgPort", pgPort);
            propObj.put("pgDb", pgDb);
            propObj.put("pgUser", pgUser);
            propObj.put("pgPwd", pgPwd);

            propObj.put("mongoHost", mongoHost);
            propObj.put("mongoPort", mongoPort);
            propObj.put("mongoDb", mongoDb);
            propObj.put("trafficStatus", trafficCol);
            propObj.put("corresponding", correspondingCol);

//            System.out.println(propObj.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {in.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
            return propObj;
        }
    }

    public static void main(String[] args) {
        System.out.println(getProperties().toString());
    }

}
